<template>
	<view class="add">
		<block v-if="!hideBox">
			<view class="add_prompt">请录入设备信息</view>
			
			<view class="add_item">
				<view>设备名称</view>
				<input type="text" :value="upJson.title" placeholder="请输入设备名称" :data-name="'title'" @input="getInputValue"/>
			</view>
			<view class="add_item">
				<view>设备品牌</view>
				<input type="text" :value="upJson.deviceBand" placeholder="请输入设备品牌" :data-name="'deviceBand'" @input="getInputValue"/>
			</view>
			
			<view class="add_item">
				<view>设备机号</view>
				<input type="text" :value="upJson.machine_number" placeholder="请输入设备机号" :data-name="'machine_number'" @input="getInputValue"/>
			</view>
			
			<view class="add_item">
				<view>设备型号</view>
				<input type="text" :value="upJson.module" placeholder="请输入设备型号" :data-name="'module'" @input="getInputValue"/>
			</view>
			
			<view class="add_item">
				<view>设备编号</view>
				<view class="add_code" @tap="getNum()">
					<image src="/static/images/code.png"></image>
					<text>{{upJson.number == "" ? '扫码显示' : upJson.number}}</text>
				</view>
			</view>
			
			<view class="add_prompt">安装信息</view>
			
			<view class="add_item">
				<view>安装日期</view>
				<picker mode="date" :data-name="'az_create_at'" @change="getInputValue">
					<text>{{upJson.az_create_at}}</text>
					<image src="/static/images/right.png" class="right"></image>
				</picker>
			</view>
			
			<view class="add_item">
				<view>质保到期时间</view>
				<picker mode="date" :data-name="'zb_create_at'" @change="getInputValue">
					<text>{{upJson.zb_create_at}}</text>
					<image src="/static/images/right.png" class="right"></image>
				</picker>
			</view>
			
			<view class="save" @tap="save()">提交</view>
		</block>
		<Loading ref="load"></Loading>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				list:[],
				nameIndex:0,
				upJson:{
					"title":"",//名称
					"deviceBand":"",//品牌
					"machine_number":"",//机号
					"module":"",//型号
					"number":"111",//编号
					// "deviceCategory":1,//分类
					"yj_create_at":"",//预警时间
					"az_create_at":"",//安装时间,时间戳
					"zb_create_at":"",//保修时间
					"customer_id":"",//商户id
				},
				hideBox:true
			}
		},
		onLoad(options) {
			this.upJson.customer_id = options.merchantId;
			this.deviceId = options.deviceId;
		},
		onShow() {
			if(this.deviceId != undefined){
				this.$refs.load.initLoading({loadType:1,loadPrompt:false}).then(res => {
					this.$api.getClientEquipmentDetail({id:this.deviceId}).then(res => {
						this.upJson = res;
						this.upJson.az_create_at = res.az_create_at.split(" ")[0];
						this.upJson.zb_create_at = res.zb_create_at.split(" ")[0];
						this.hideBox = false;
						this.$refs.load.getLoadingData({})
					})
				})
			}else{
				this.upJson.yj_create_at = this.$api.getNowTime();
				this.upJson.az_create_at = this.$api.getNowTime();
				this.upJson.zb_create_at = this.$api.getNowTime();
				this.hideBox = false;
			}
		},
		methods:{
			getInputValue(e){//获取数据
				this.upJson[e.currentTarget.dataset.name] = e.detail.value;
			},
			getNum(){//获取二维码
				uni.scanCode({
					success(res) {
						console.log(res)
						// this.upJson.number = '12121';
					}
				})
			},
			save(){//提交
				if(this.upJson.deviceBand == ""){
					uni.showToast({title:"设备品牌不能为空",icon:"none"})
				}else if(this.upJson.machine_number == ""){
					uni.showToast({title:"设备机号不能为空",icon:"none"})
				}else if(this.upJson.module == ""){
					uni.showToast({title:"设备型号不能为空",icon:"none"})
				}else if(this.upJson.number == ""){
					uni.showToast({title:"设备编码不能为空",icon:"none"})
				}else{
					let upJson = {};
					for(let i in this.upJson){
						upJson[i] = this.upJson[i];
					}
					this.deviceId == undefined ? this.$api.addClientEquipment(upJson) : this.$api.editClientEquipment(upJson);
					this.$app.globalData.upData.clientInfo[1] = true;
				}
			}
		}
	}
</script>

<style>
	.add{background: #FFFFFF;padding-bottom: 50upx;}
</style>
